
#include <vector>

#include <boost/algorithm/string.hpp>

#include "bocat/common/base64.hpp"

#include "fc_indexiter.hpp"

namespace	sakcab	{

FileCellIndexIterator::FileCellIndexIterator( const char* filename )
		:	_ifs( filename )
{
}

FileCellIndexIterator::~FileCellIndexIterator()
{
	if ( _ifs.is_open() ) {
		_ifs.close();
	}
}


///////////////////////////////////////////////////////////////////////
//


int	
FileCellIndexIterator::next( std::string& kpin, std::string& skey, std::string& key )
{
	if ( !_ifs ) {
		return	0;
	}
	if ( !_ifs.is_open() ) {
		return	0;
	}
	if ( _ifs.eof() ) {
		return	0;
	}

	while ( !_ifs.eof() ) {
		std::string line;
		std::getline( _ifs, line );

		std::vector< std::string >  fields;
		boost::split(fields, line, boost::is_any_of("\t"));
		if ( fields.size() != 5 ) { 
			continue;
		}   

		skey = bocat::common::base64_decode< std::string >( fields[ 0 ] );
		kpin = bocat::common::base64_decode< std::string >( fields[ 1 ] );
		key = fields[ 3 ];

		return	1;
	}

    return  0;  
}


}	//// namespace sakcab


